<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .title {
            width: 100%;
            text-align: center;
            margin: 20px 0;
        }

        .box {
            width: 1000px;
            height: auto;
            margin: 0 auto;
        }

        .item {
            display: flex;
            width: 100%;
            height: auto;
            padding: 10px;
            border: 1px solid black;
            margin-bottom: 10px;
        }

        .left {
            width: 800px;
            height: 100%;
        }

        .right {
            width: 200px;
            height: 100%;
        }

        .bottom {
            position: fixed;
            width: 600px;
            height: 150px;
            left: 50%;
            margin-left: -300px;
            bottom: 0;
            border: 1px solid black;
            text-align: center;
            line-height: 25px;
        }

        .buy {
            padding: 5px 10px;
            margin: 10px;
            font-size: 20px;
        }
    </style>
</head>

<body>
<div id="app">
    <h1 class="title">我的购物车</h1>
    <div class="box">
        <div v-for="(item, index) in cart" :key="item.goods.id" class="item">
            <div class="left">
                <p>商品名称：{{item.goods.name}}</p>
                <p>剩余数量：{{item.goods.quantity}}</p>
                <p>描述：{{item.goods.resume}}</p>
                <p>价格：{{item.goods.price}}￥</p>
            </div>
            <div class="right">
                <button @click="del(item.goods.id)" class="buy">-</button>
                <span>{{item.cart.number}}</span>
                <button @click="add(item.goods.id)" class="buy">+</button>
            </div>
        </div>

        <div class="bottom">
            <p>已选{{countNum}}件商品</p>
            <p>共计{{countPrice}}元</p>
            <p>
                <button @click="buyAll">买！</button>
            </p>
            <p>
                <button @click="seeBuy">看看其他商品</button>
            </p>
            <p>
                <button @click="logout">退出登录</button>
            </p>
        </div>
    </div>
</div>

<script src="./js/vue.js"></script>
<script src="./js/axios.js"></script>
<script src="./js/constant.js"></script>
<script>
    new Vue({
        el: "#app",
        data: {
            cart: [],
            countNum: 0,
            countPrice: 0,
        },
        methods: {
            add(id) {
                let data = new URLSearchParams();
                data.append("goodsId", id);

                axios
                    .post(baseUrl + "buybuybuy/api/cart/increase", data)
                    .then((res) => {
                        let data = res.data;
                        if (data.success) {
                            this.getCart();
                        } else {
                            alert(data.msg);
                        }
                    });
            },
            del(id) {
                let data = new URLSearchParams();
                data.append("goodsId", id);

                axios
                    .post(baseUrl + "buybuybuy/api/cart/decrease", data)
                    .then((res) => {
                        let data = res.data;
                        if (data.success) {
                            this.getCart();
                        } else {
                            alert(data.msg);
                        }
                    });
            },
            getCart() {
                // 获取购物车商品
                axios
                    .get(baseUrl + "buybuybuy/api/cart/list")
                    .then((res) => {
                        let data = res.data;
                        if (data.success) {
                            this.cart = data.data.cartGoods;
                            this.countNum = data.data.countNum;
                            this.countPrice = data.data.countPrice;
                        } else {
                            alert(data.msg);
                        }
                    })
                    .catch((res) => {
                        if (res.status == 403) {
                            alert("请先登录");
                            location = "./login.html";
                        }
                    });
            },
            logout() {
                axios
                    .get(baseUrl + "buybuybuy/api/user/logout")
                    .then((res) => {
                        let data = res.data;
                        if (data.success) {
                            alert("退出成功，请重新登录");
                            location = "./login.html";
                        } else {
                            alert(data.msg);
                        }
                    });
            },
            seeBuy() {
                location = "./buy.html";
            },
            buyAll() {
                axios
                    .post(baseUrl + "buybuybuy/api/cart/buy")
                    .then((res) => {
                        let data = res.data;
                        if (data.success) {
                            alert("购买成功！")
                            this.getCart();
                        } else {
                            alert(data.msg);
                        }
                    });
            },
        },
        mounted() {
            this.getCart();
        },
    });
</script>
</body>
</html>
